This tutorial illustrates the core visualization utilities available in Ax.
import numpy as np
from ax.service.ax_client import AxClient
from ax.modelbridge.cross_validation import cross_validate
from ax.plot.contour import interact_contour
from ax.plot.diagnostic import interact_cross_validation
from ax.plot.scatter import(
interact_fitted,
plot_objective_vs_constraints,
tile_fitted,
)
from ax.plot.slice import plot_slice
from ax.utils.measurement.synthetic_functions import hartmann6
from ax.utils.notebook.plotting import render, init_notebook_plotting
init_notebook_plotting()
[INFO 08-26 05:41:59] ax.utils.notebook.plotting: Injecting Plotly library into cell. Do not overwrite or delete cell.
The vizualizations require an experiment object and a model fit on the evaluated data. The routine below is a copy of the Service API tutorial, so the explanation here is omitted. Retrieving the experiment and model objects for each API paradigm is shown in the respective tutorials
noise_sd = 0.1
param_names = [f"x{i+1}" for i in range(6)] # x1, x2, ..., x6
def noisy_hartmann_evaluation_function(parameterization):
x = np.array([parameterization.get(p_name) for p_name in param_names])
noise1, noise2 = np.random.normal(0, noise_sd, 2)
return {
"hartmann6": (hartmann6(x) + noise1, noise_sd),
"l2norm": (np.sqrt((x ** 2).sum()) + noise2, noise_sd)
}
ax_client = AxClient()
ax_client.create_experiment(
name="test_visualizations",
parameters=[
{
"name": p_name,
"type": "range",
"bounds": [0.0, 1.0],
}
for p_name in param_names
],
objective_name="hartmann6",
minimize=True,
outcome_constraints=["l2norm <= 1.25"]
)
[INFO 08-26 05:41:59] ax.service.ax_client: Starting optimization with verbose logging. To disable logging, set the `verbose_logging` argument to `False`. Note that float values in the logs are rounded to 6 decimal points.
[INFO 08-26 05:41:59] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x1. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-26 05:41:59] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x2. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-26 05:41:59] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x3. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-26 05:41:59] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x4. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-26 05:41:59] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x5. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-26 05:41:59] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x6. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 08-26 05:41:59] ax.service.utils.instantiation: Created search space: SearchSpace(parameters=[RangeParameter(name='x1', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x2', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x3', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x4', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x5', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x6', parameter_type=FLOAT, range=[0.0, 1.0])], parameter_constraints=[]).
[INFO 08-26 05:41:59] ax.modelbridge.dispatch_utils: Using Bayesian optimization since there are more ordered parameters than there are categories for the unordered categorical parameters.
[INFO 08-26 05:41:59] ax.modelbridge.dispatch_utils: Using Bayesian Optimization generation strategy: GenerationStrategy(name='Sobol+GPEI', steps=[Sobol for 12 trials, GPEI for subsequent trials]). Iterations after 12 will take longer to generate due to model-fitting.
for i in range(20):
parameters, trial_index = ax_client.get_next_trial()
# Local evaluation here can be replaced with deployment to external system.
ax_client.complete_trial(trial_index=trial_index, raw_data=noisy_hartmann_evaluation_function(parameters))
[INFO 08-26 05:41:59] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.950698, 'x2': 0.621987, 'x3': 0.450424, 'x4': 0.155183, 'x5': 0.737592, 'x6': 0.390354}.
[INFO 08-26 05:41:59] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-0.067258, 0.1), 'l2norm': (1.535551, 0.1)}.
[INFO 08-26 05:41:59] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.956652, 'x2': 0.114098, 'x3': 0.614066, 'x4': 0.173691, 'x5': 0.713058, 'x6': 0.197443}.
[INFO 08-26 05:41:59] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (0.030155, 0.1), 'l2norm': (1.410137, 0.1)}.
[INFO 08-26 05:41:59] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.64739, 'x2': 0.972102, 'x3': 0.899264, 'x4': 0.952417, 'x5': 0.396469, 'x6': 0.57374}.
[INFO 08-26 05:41:59] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (0.0122, 0.1), 'l2norm': (1.960648, 0.1)}.
[INFO 08-26 05:41:59] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.815065, 'x2': 0.609866, 'x3': 0.004613, 'x4': 0.29321, 'x5': 0.75764, 'x6': 0.772672}.
[INFO 08-26 05:41:59] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (-0.154448, 0.1), 'l2norm': (1.552822, 0.1)}.
[INFO 08-26 05:42:00] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.326678, 'x2': 0.843802, 'x3': 0.417863, 'x4': 0.76472, 'x5': 0.896209, 'x6': 0.896977}.
[INFO 08-26 05:42:00] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (0.05486, 0.1), 'l2norm': (1.765454, 0.1)}.
[INFO 08-26 05:42:00] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.104478, 'x2': 0.085309, 'x3': 0.462542, 'x4': 0.465972, 'x5': 0.604237, 'x6': 0.124589}.
[INFO 08-26 05:42:00] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (-0.247452, 0.1), 'l2norm': (0.990426, 0.1)}.
[INFO 08-26 05:42:00] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.115003, 'x2': 0.657785, 'x3': 0.499991, 'x4': 0.481579, 'x5': 0.093136, 'x6': 0.327477}.
[INFO 08-26 05:42:00] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (-0.387638, 0.1), 'l2norm': (1.050924, 0.1)}.
[INFO 08-26 05:42:00] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.512675, 'x2': 0.764089, 'x3': 0.621671, 'x4': 0.65737, 'x5': 0.78051, 'x6': 0.268176}.
[INFO 08-26 05:42:00] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (-1.154104, 0.1), 'l2norm': (1.503504, 0.1)}.
[INFO 08-26 05:42:00] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.15903, 'x2': 0.510715, 'x3': 0.757822, 'x4': 0.049338, 'x5': 0.562037, 'x6': 0.211284}.
[INFO 08-26 05:42:00] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (-0.194523, 0.1), 'l2norm': (1.081579, 0.1)}.
[INFO 08-26 05:42:00] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.631572, 'x2': 0.192298, 'x3': 0.171419, 'x4': 0.656879, 'x5': 0.373791, 'x6': 0.937154}.
[INFO 08-26 05:42:00] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (-0.205729, 0.1), 'l2norm': (1.341928, 0.1)}.
[INFO 08-26 05:42:00] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.75441, 'x2': 0.63411, 'x3': 0.807903, 'x4': 0.15843, 'x5': 0.922451, 'x6': 0.134279}.
[INFO 08-26 05:42:00] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (-0.007858, 0.1), 'l2norm': (1.558818, 0.1)}.
[INFO 08-26 05:42:00] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.810177, 'x2': 0.618787, 'x3': 0.277954, 'x4': 0.398746, 'x5': 0.648519, 'x6': 0.965405}.
[INFO 08-26 05:42:00] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (-0.037876, 0.1), 'l2norm': (1.559641, 0.1)}.
[INFO 08-26 05:42:08] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.282319, 'x2': 0.59585, 'x3': 0.577431, 'x4': 0.597657, 'x5': 0.629282, 'x6': 0.220704}.
[INFO 08-26 05:42:08] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-0.805959, 0.1), 'l2norm': (1.397133, 0.1)}.
[INFO 08-26 05:42:27] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.19942, 'x2': 0.453633, 'x3': 0.576822, 'x4': 0.580807, 'x5': 0.514744, 'x6': 0.238047}.
[INFO 08-26 05:42:28] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (-0.34653, 0.1), 'l2norm': (1.158786, 0.1)}.
[INFO 08-26 05:42:39] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.170993, 'x2': 0.700787, 'x3': 0.546561, 'x4': 0.55089, 'x5': 0.51997, 'x6': 0.203018}.
[INFO 08-26 05:42:39] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-0.671631, 0.1), 'l2norm': (1.181508, 0.1)}.
[INFO 08-26 05:42:42] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.136846, 'x2': 0.673158, 'x3': 0.710875, 'x4': 0.536344, 'x5': 0.575196, 'x6': 0.253932}.
[INFO 08-26 05:42:42] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-0.585264, 0.1), 'l2norm': (1.337166, 0.1)}.
[INFO 08-26 05:43:20] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.319816, 'x2': 0.785641, 'x3': 0.404457, 'x4': 0.612904, 'x5': 0.577959, 'x6': 0.195139}.
[INFO 08-26 05:43:20] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-1.918501, 0.1), 'l2norm': (1.251836, 0.1)}.
[INFO 08-26 05:43:38] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.295638, 'x2': 0.768546, 'x3': 0.353327, 'x4': 0.571577, 'x5': 0.453737, 'x6': 0.17446}.
[INFO 08-26 05:43:38] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-1.815091, 0.1), 'l2norm': (1.087958, 0.1)}.
[INFO 08-26 05:44:11] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.358269, 'x2': 0.817941, 'x3': 0.353383, 'x4': 0.580103, 'x5': 0.485106, 'x6': 0.142663}.
[INFO 08-26 05:44:11] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-2.587478, 0.1), 'l2norm': (1.396016, 0.1)}.
[INFO 08-26 05:44:45] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.358706, 'x2': 0.766335, 'x3': 0.317988, 'x4': 0.497782, 'x5': 0.358468, 'x6': 0.141512}.
[INFO 08-26 05:44:45] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-2.328001, 0.1), 'l2norm': (1.085186, 0.1)}.
The plot below shows the response surface for hartmann6 metric as a function of the x1, x2 parameters.
The other parameters are fixed in the middle of their respective ranges, which in this example is 0.5 for all of them.
# this could alternately be done with `ax.plot.contour.plot_contour`
render(ax_client.get_contour_plot(param_x="x1", param_y="x2", metric_name='hartmann6'))
[INFO 08-26 05:44:45] ax.service.ax_client: Retrieving contour plot with parameter 'x1' on X-axis and 'x2' on Y-axis, for metric 'hartmann6'. Remaining parameters are affixed to the middle of their range.
The plot below allows toggling between different pairs of parameters to view the contours.
model = ax_client.generation_strategy.model
render(interact_contour(model=model, metric_name='hartmann6'))
This plot illustrates the tradeoffs achievable for 2 different metrics. The plot takes the x-axis metric as input (usually the objective) and allows toggling among all other metrics for the y-axis.
This is useful to get a sense of the pareto frontier (i.e. what is the best objective value achievable for different bounds on the constraint)
render(plot_objective_vs_constraints(model, 'hartmann6', rel=False))
CV plots are useful to check how well the model predictions calibrate against the actual measurements. If all points are close to the dashed line, then the model is a good predictor of the real data.
cv_results = cross_validate(model)
render(interact_cross_validation(cv_results))
Slice plots show the metric outcome as a function of one parameter while fixing the others. They serve a similar function as contour plots.
render(plot_slice(model, "x2", "hartmann6"))
Tile plots are useful for viewing the effect of each arm.
render(interact_fitted(model, rel=False))
Total runtime of script: 3 minutes, 5.06 seconds.